package com.thinkersre.service;

import com.thinkersre.util.RedisKeyUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Service
public class DataService {
    @Resource
    private RedisTemplate redisTemplate;
    // 将Date类型转化为String类型
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    //将指定ip计入UV//获取访问量
    /*********************** HypeLogLog*************************/
    // 将指定ip计入UV---k:当前时间 v:ip
    public void recordUV(String ip){
        String redisKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(new Date()));
        redisTemplate.opsForHyperLogLog().add(redisKey,ip);
    }
    //统计指定日期范围内的UV
    // 统计指定日期范围内的ip访问数UV
    public long calculateUV(Date start,Date end){
        if(start == null||end == null){
            throw new IllegalArgumentException("参数不能为空!");
        }
        //整理该日期范围内的key
        List<String> keyList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while(!calendar.getTime().after(end)){
            // 获取该日期范围内的每一天的Key存入集合
            String key = RedisKeyUtil.getUVKey(simpleDateFormat.format(calendar.getTime()));
            keyList.add(key);
            // 日期+1(按照日历格式)
            calendar.add(Calendar.DATE,1);
        }
        //合并数据
        // 合并日期范围内相同的ip
        String redisKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(start),simpleDateFormat.format(end));
        // 获取keyList中的每一列key进行合并
        redisTemplate.opsForHyperLogLog().union(redisKey,keyList.toArray());
        //返回统计的结果
        return redisTemplate.opsForHyperLogLog().size(redisKey);
    }
    //将指定用户计入DAU//获取用户访问量
    public void recordDAU(int userId){
        String redisKey = RedisKeyUtil.getDAUKey(simpleDateFormat.format(new Date()));
        redisTemplate.opsForValue().setBit(redisKey,userId,true);
    }
    //统计指定日期范围内的DAU//统计用户访问量
    public long calculateDAU(Date start,Date end){
        if(start == null||end == null){
            throw new IllegalArgumentException("参数不能为空!");
        }
//        if (end.before(start)) {
//            throw new IllegalArgumentException("请输入正确的时间段！");
//        }
        //整理该日期范围内的key
        List<byte[]> keyList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while(!calendar.getTime().after(end)){
            String key = RedisKeyUtil.getDAUKey(simpleDateFormat.format(calendar.getTime()));
            keyList.add(key.getBytes());
            // 日期+1(按照日历格式)
            calendar.add(Calendar.DATE,1);
        }
        //进行or运算
        return (long)redisTemplate.execute(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
                String redisKey = RedisKeyUtil.getDAUKey(simpleDateFormat.format(start),simpleDateFormat.format(end));
                redisConnection.bitOp(RedisStringCommands.BitOperation.OR,
                        redisKey.getBytes(),keyList.toArray(new byte[0][0]));
                return redisConnection.bitCount(redisKey.getBytes());
            }
        });
    }
}
